import java.util.Arrays;

/**
 * O(N^2)
 */
class Solution {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int maxVal = nums.length-1;
        int count = 0;
        while (maxVal >= 2){
            int left = 0;//
            int right = maxVal-1;
            while (left != right) {
                if (nums[left] + nums[right] > nums[maxVal]) {
                    count += right-left;
                    right--;
                }else {
                    left++;
                }
            }
            maxVal--;
        }
        return count;
    }
}